﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsideDL.MISService;
using InsideDL.MISControl;
using InsideDL.MISData;


namespace InsideDL
{
    public partial class frmIG004 : MISControl.frmMatrix
    {
        EXLAPP exlAPP = null;
        bool isJan = false;
        public frmIG004()
        {
            InitializeComponent();
            Utility.LoadConfigValue("frmIG004");
            this.controlList = new List<ucSelFileOrPath>
            {
                ucFYP,ucFinal
            };
            ucExecute1.ExecuteClick += new EventHandler(ucClickFacade);
            ucExecute_Exit.ExecuteClick += new EventHandler(ucClickFacade);

        }

        private void frmIG004_Load(object sender, EventArgs e)
        {

        }

        protected override void execute()
        {
            if (base.Vaildation().Equals(false))
            { return; }

            string SourceShtCM, SourceShtYTD, TargetShtCM, TargetShtYTD, COs
                , IncomeCM, IncomeYTD, StartColYTD, StartColCM, month, rng;
            
            TargetShtCM = Utility.GetConfigValue("TargetShtCM");
            COs = Utility.GetConfigValue("COs");
            SourceShtCM = Utility.GetConfigValue("SourceShtCM");
            SourceShtYTD = Utility.GetConfigValue("SourceShtYTD");
            IncomeCM = Utility.GetConfigValue("IncomeCM");
            IncomeYTD = Utility.GetConfigValue("IncomeYTD");
            TargetShtYTD = Utility.GetConfigValue("TargetShtYTD");
            StartColYTD = Utility.GetConfigValue("StartColYTD");
            StartColCM = Utility.GetConfigValue("StartColCM");

            month = getFinalMonth(ucFinal.Txt);
            //rng = Utility.GetNewRange(StartCol, StartRow, Convert.ToInt16(month));
            //金控經營委員會(人壽)10005_Final
            
            DataIG004 ig004 = new DataIG004();
            exlAPP = new EXLAPP();
            exlAPP.OpenFile(ucFinal.Txt);
            exlAPP.OpenFile(ucFYP.Txt);

            string[] coList = ig004.GetInsuranceCO(
                exlAPP.GetSheetRangeArray(TargetShtCM, COs));
            coList[2] = "中國"; //2012-04-30 把來源中國人壽置換成中國
            DataTable incomeCMDT = ig004.GetInsuranceIncome(
                exlAPP.GetSheetRangeArray(SourceShtCM, IncomeCM));

             DataTable incomeYTDDT = null;

             if (isJan.Equals(false))
             {
                 incomeYTDDT = ig004.GetInsuranceIncome(
                    exlAPP.GetSheetRangeArray(SourceShtYTD, IncomeYTD));
             }
             

            DataTable[] incomeDTs = {incomeCMDT,incomeYTDDT };
            string[] targets = { TargetShtCM,TargetShtYTD };
            string[] startCols = {StartColCM,StartColYTD };

            for (int i = 0; i < targets.Length; i++)
            {
                if (incomeDTs[i] != null)
                {
                    int StartRow = Convert.ToInt16(Utility.GetConfigValue("StartRow"));
                    foreach (string co in coList)
                    {
                        IQueryable<DataRow> incomeRow = ig004.GetSeleIncome(co, incomeDTs[i]);
                        foreach (DataRow row in incomeRow)
                        {
                            rng = Utility.GetNewRange(startCols[i], StartRow.ToString()
                                , Convert.ToInt16(month));
                            exlAPP.SetRangeValue(targets[i], rng, row[1].ToString());
                            StartRow++;
                        }
                    }            
                }
            }
            exlAPP.SaveCloseXLS();
            exlAPP = null;
            showMessage(this.Text, "完成！！");            
        }


        protected string getFinalMonth(string p_final)
        {
            string startYear = Utility.GetConfigValue("StartYear");

            //"金控經營委員會(人壽)10005_Final"
            string[] tmp = p_final.Split(new[] { ')' });
            tmp = tmp[1].Split(new[] { '_' });
            int x,y;
            x = Convert.ToInt16(tmp[0].Substring(0, 3)) - 
                Convert.ToInt16(startYear);
            isJan = Convert.ToInt16(tmp[0].Substring(4)).Equals(1) ? true:false ;
            y = x > 0 ? 
                12 * x + Convert.ToInt16(tmp[0].Substring(3))
                : 0;

            //return tmp[0].Substring(3);
            return y.ToString();
        }
    }
}
